{==============================================================================}
{  Acroprise Multi-Language Components                                         }
{                                                                              }
{  Copyright (C), 1999-2012, Acroprise Inc.                                    }
{  http://www.acroprise.com.tw                                                 }
{  ALL RIGHTS RESERVED                                                         }
{                                                                              }
{  Author   : Mustapha.Wang                                                    }
{  Version  : 4.1.15.2159                                                      }
{  Creation : 2002/05/06                                                       }
{  Revision :                                                                  }
{  Task     :                                                                  }
{==============================================================================}
unit acroMultiDBDD;
{$I acroMultiLang.Inc}
interface
uses
  Classes,DB,SysUtils,acroMultiDD,acroMultiDBDDReader,acroMultiDDReader;
type
  TacroMultiDBDD=class(TCustomMultiDD)
  private
    FDataSet:TDataSet;
    procedure SetDataSet(const Value: TDataSet);
  protected
    procedure BuildDictionary;override;
    procedure _ImportFromDB(ADataSet:TDataSet;AFieldsMapping:TDDFieldsMapping;
      AOverride,AExistsException:Boolean);virtual;
  public
    procedure Notification(AComponent:TComponent;Operation:TOperation);override;
    procedure ImportFromDB(ADataSet:TDataSet;AFieldsMapping:TDDFieldsMapping;
      AOverride,AExistsException:Boolean);virtual;
  published
    property DDName;
    property DataSet:TDataSet read FDataSet write SetDataSet;
    property UseDefaultFields;
    property FieldsMapping;
    property IsUnicode;
    property DDKeyCodePage;
    property Events;
    property IsLoadAllFieldIntoHash;
  end;

  procedure ReplaceDataSetContent(ADataSet:TDataSet;Fields,ADDName,ALCID:string);
  
implementation

procedure ReplaceDataSetContent(ADataSet:TDataSet;Fields,ADDName,ALCID:string);
var
  FieldList:TStringList;
  i,Index:integer;
  AD:TCustomMultiDD;
begin
  AD:=acroDDStorage.FindDD(ADDName);
  if AD=nil then exit;
  if Fields='' then exit;
  FieldList:=TStringList.Create;
  ADataSet.DisableControls;
  try
    Fields:=Fields+';';
    Index:=1;
    for i:=1 to Length(Fields) do
    begin
      if Fields[i]=';' then
      begin
        FieldList.Add(Copy(Fields,Index,i-Index));
        Index:=i+1;
      end;
    end;
    ADataSet.First;
    while not ADataSet.Eof do
    begin
      ADataSet.Edit;
      for i:=0 to FieldList.Count-1 do
      begin
        ADataSet.FieldByName(FieldList[i]).AsString:=AD.GetDisplayValue(ADataSet.FieldByName(FieldList[i]).AsString,ALCID,'0');
      end;
      ADataSet.Post;
      ADataSet.Next;
    end;
  finally
    FieldList.Free;
    ADataSet.EnableControls;
  end;
end;

{ TacroMultiDBDD }

procedure TacroMultiDBDD.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if Operation<>opRemove then exit;
  if AComponent=FDataSet then FDataSet:=nil;
end;

procedure TacroMultiDBDD.BuildDictionary;
begin
  if UseDefaultFields then
    _ImportFromDB(FDataSet,nil,false,true)
  else _ImportFromDB(FDataSet,FieldsMapping,false,true);
end;

procedure TacroMultiDBDD.SetDataSet(const Value: TDataSet);
begin
  FDataSet := Value;
  if Value<>nil then Value.FreeNotification(Self);
end;

procedure TacroMultiDBDD.ImportFromDB(ADataSet: TDataSet;
  AFieldsMapping: TDDFieldsMapping; AOverride,AExistsException: Boolean);
begin
  _ImportFromDB(ADataSet,AFieldsMapping,AOverride,AExistsException);
end;

procedure TacroMultiDBDD._ImportFromDB(ADataSet: TDataSet;
  AFieldsMapping: TDDFieldsMapping; AOverride,AExistsException: Boolean);
var
  IDR:IDataRead;
begin
  IDR:=TDBDDReader.Create(ADataSet,AFieldsMapping);
  try
    _ImportFromReader(IDR,AFieldsMapping,AOverride,AExistsException);
  finally
    IDR:=nil;
  end;
end;

end.
